//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js');
Page({
  data: {
    UserID: 0, //用户id
    WeChartUserID: 0, //微信用户id
    VehicleStatus: 0, //认证级别
    headImg: "/images/default-head.png",
    gender: 0, //用户性别   0  暂无   1男   2女
    userName: "注册/登录",
    carInfo: [], //车辆线下
    hotMobiles: [], //紧急电话
    NickName: '未注册'
  },
  onLoad: function (options) {
    //this.getHotMobiles();
  },
  //页面每次出现就更新用户信息
  onShow: function () {
    let self = this;
    app.globalData.isCurCarGroupList = false; //重置
    app.getSession().then(function () {
      self.setData({
        UserID: app.globalData.userInfo.UserID,
        headImg: app.globalData.userInfo.UserHead || '/images/default-head.png',
        userName: app.globalData.userInfo.UserName || "注册/登录",
        gender: app.globalData.userInfo.Sex,
        WeChartUserID: app.globalData.userInfo.WeChartUserID,
        VehicleStatus: app.globalData.userInfo.VehicleStatus,
        NickName: app.globalData.userInfo.NickName || '未注册'
      });
      self.getCars();
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.globalData.shareInfo;
  },
  //绑定车辆
  bindCarTap: function () {
    let self = this;
    if (self.data.UserID < 1) {
      wx.navigateTo({
        url: '/pages/index/index',
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/ucenter/drvlicense/drvlicense',
    });
  },
  onPageScroll: function (e) {
    let scrollTop = e.scrollTop;
    this.setData({
      scrollTop: scrollTop
    })
  },
  /**
   * 获取用户信息
   */
  getUserInfo: function (res) {
    let self = this;
    if (res.detail.errMsg == 'getUserInfo:ok') {
      //获取授权码
      app.checkSession().then(function () {
        app.requestUser(res.detail.userInfo).then(
          function () {
            self.setData({
              headImg: app.globalData.userInfo.UserHead,
              userName: app.globalData.userInfo.NickName,
              NickName: app.globalData.userInfo.NickName,
              gender: app.globalData.userInfo.Sex
            });
          }
        );
      })
    }
  },
  /**
   * 认证
   */
  loginTap: function (e) {
    //获取电话-添加车辆-获取昵称-更新用户信息  优先级
    let self = this;
    if (self.data.UserID < 1) {
      wx.navigateTo({
        url: '/pages/index/index',
      })
      return;
    }
    if (self.data.VehicleStatus < 1) {
      wx.navigateTo({
        url: '/pages/ucenter/drvlicense/drvlicense',
      });
    } else {
      wx.navigateTo({
        url: '/pages/ucenter/userInfoList/userInfoList',
      });
    }
  },
  /**
   * 获取车辆信息
   */
  getCars: function () {
    let self = this;
    let url = app.globalData.apiRootUrl + '/User/VehicleLicenses';
    app.httpRequest({
      url: url,
      data: {
        UserID: app.globalData.userInfo.UserID
      },
      success: function (res) {
        let rdata = res.data;
        if (rdata.status == 0 && rdata.data) {
          let tps = [];
          for (let i = 0; i < rdata.data.length; i++) {
            tps.push({
              carNumber: rdata.data[i].PlateNo,
              carModel: rdata.data[i].Brand + ' ' + rdata.data[i].Model,
              VehicleLicenseID: rdata.data[i].VehicleLicenseID,
              carModelImg: app.globalData.apiImgUrl + 'car/m_' + rdata.data[i].BrandID + '_100.png'
            });
          }
          self.setData({
            carInfo: tps
          })
        } else {
          wx.showToast({
            title: '车辆信息获取失败',
            icon: 'none'
          })
        }
      }
    });
  },
  /**
   * 行驶证详情
   */
  vehDetail: function (e) {
    wx.navigateTo({
      url: '/pages/ucenter/carSetting/carSetting?id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 紧急电话
   */
  getHotMobiles: function () {
    let self = this;
    let url = app.globalData.apiRootUrl + '/Common/HotMobiles';
    app.httpRequest({
      url: url,
      success: function (res) {
        let rdata = res.data;
        let tmps = [];
        for (let i = 0; i < rdata.data.length; i++) {
          let tmp = self.isExits(tmps, rdata.data[i]);
          if (tmp == null) {
            tmps.push({
              MobileLogoUrl: rdata.data[i].MobileLogoUrl,
              MobileGroupName: rdata.data[i].MobileGroupName,
              MobileGroup: rdata.data[i].MobileGroup,
              Chldren: [{
                MobileDesc: rdata.data[i].MobileDesc,
                Mobile: rdata.data[i].Mobile
              }]
            });
          } else {
            tmp.Chldren.push({
              MobileDesc: rdata.data[i].MobileDesc,
              Mobile: rdata.data[i].Mobile
            });
          }
        }
        self.setData({
          hotMobiles: tmps
        })
      }
    });

  },
  isExits: function (tmps, item) {
    for (let i = 0; i < tmps.length; i++) {
      if (item.MobileGroup == tmps[i].MobileGroup) {
        return tmps[i];
      }
    }
    return null;
  },
  /**
   * 拨打电话
   */
  mobileTap: function (e) {
    let self = this;
    switch (e.currentTarget.dataset.gtype) {
      case 1: //道路救援
        wx.navigateTo({
          url: '/pages/ucenter/roadServer/roadServer',
        })
        break;
      case 2: //保险报案
        wx.navigateTo({
          url: '/pages/ucenter/insureCallList/insureCallList',
        })
        break;
      case 3: //交警报案
        wx.makePhoneCall({
          phoneNumber: '122' //仅为示例，并非真实的电话号码
        });
        break;
      case 4: //挪车电话
        wx.makePhoneCall({
          phoneNumber: '114' //仅为示例，并非真实的电话号码
        });
        break;
    }
  },
  /**
   * webview跳转
   */
  webViewTap: function (e) {
    let self = this;
    let url = '';
    let baseUrl = "https://m.banmachewu.com";
    let orderUrl = "https://m.banmachewu.com/customerV2/pages/order";
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case 'violation': //违章查询
        url = baseUrl + '/customer-v1.0/violation/search.aspx';
        break;
      case 'vehicle': //预约检测场
        url = baseUrl + "/vehicle/index.aspx";
        break;
      case 'validateTheCar': //代办验车
        url = orderUrl + '/ValidateTheCar.aspx';
        break;
      case 'standard': //查排放标准
        url = baseUrl + "/standard/";
        break;
    }
    let query = '?webUrl=' + encodeURIComponent(url);
    wx.navigateTo({
      url: '/pages/webView/webView' + query
    })

  },
  /**
   * 敬请期待
   */
  waitForTap: function () {
    let self = this;
    wx.navigateTo({
      url: '/pages/ucenter/waitingFor/waitingFor',
    })
  },
  /**
   * 我的社群
   */
  myGroupTap: function () {
    let self = this;
    if (self.data.WeChartUserID < 1) {
      wx.navigateTo({
        url: '/pages/ucenter/login/login',
      })
      return;
    }
    if (self.data.VehicleStatus < 1) {
      wx.navigateTo({
        url: '/pages/ucenter/drvlicense/drvlicense',
      });
    } else {
      wx.navigateTo({
        url: '/pages/groupSpace/communitySquare/communitySquare?CarGroupID=' + app.globalData.userInfo.CarGroupIds[0],
      })
    }
  },
  /**
   * 前往地锁
   */
  groundLockTap: function () {
    let self = this;
    if (self.data.UserID) { //如果登陆成功
      wx.navigateTo({
        url: '/pages/utilPage/pages/groundLock/groundLock',
      });
    } else {
      self.loginTap();
    }
  },
  /**
     * 直播
     * @param {*} e 
     */
    pusherTap: function (e) {
      var that = this;
      wx.navigateTo({
        url: '/pages/live/pages/room/room',
    })
  },
  /**
   * 观看
   * @param {} e 
   */
  listTap: function (e) {
      var that = this;
      wx.navigateTo({
          url: '/pages/live/pages/index',
      })
  },
   /**
   * 观看
   * @param {} e 
   */
  playerTap: function (e) {
    var that = this;
    wx.navigateTo({
        url: '/pages/live/pages/player/player',
    })
},
})